<template>
  <div class="consult_page">
    <div class="consult_cont">
      <div class="input_box dis_flex just_center">
        <searchInput
          class="search_input"
          type="text"
          v-model="value"
          placeholder="搜索咨询内容"
          @input="handleSearch"
        >
        </searchInput>
        <div class="dis_flex" @click="isShowFilter = true">
          <div class="filtrate_txt">筛选</div>
          <img
            src="@/assets/images/evaluate/filtrate_icon.png"
            alt=""
            class="filtrate_img"
          />
        </div>
      </div>
      <div class="record_list" @scroll="handleScroll">
        <div
          v-for="(item, index) in consultRecordList"
          :key="index"
          class="item_record dis_flex"
          @touchstart="startLongPress(item)"
          @touchend="endLongPress"
          @touchcancel="endLongPress"
          @click="checkDetails(item)"
        >
          <div class="img_box">
            <img
              :src="joinUrlImg(item.consult_avatar)"
              alt=""
              class="item_img"
            />

            <div class="red_dot" v-if="item.user_dot == 1"></div>
          </div>
          <div class="felx1">
            <div class="dis_flex just_between">
              <div class="dis_flex">
                <div class="item_name">{{ item.consult_name }}</div>
                <img
                  src="@/assets/images/common/female.png"
                  alt=""
                  class="sex_img"
                />
                <!-- <img src="@/assets/images/common/man.png" alt="" class="sex_img" /> -->
                <div class="item_age">{{ item.consult_age }}岁</div>

                <div class="item_state state_02" v-if="item.is_end == 1">
                  已完结
                </div>
                <div class="item_state state_01" v-else>沟通中</div>
              </div>
              <div class="item_time">{{ timestampToUTCDate(item.time) }}</div>
            </div>
            <div class="item_info">{{ item.question }}</div>
          </div>
        </div>
        <div class="no_data_txt">-- 无更多咨询记录了 --</div>
      </div>
    </div>

    <!-- 筛选弹框 -->
    <filterPop
      :isShowPop="isShowFilter"
      @closeFilterPop="closeFilterPop"
      :type="3"
    />
    <!-- 操作弹框 -->
    <recordOperation
      :isShowOperationPop="isShowOperationPop"
      :type="3"
      @buttonOperation="buttonOperation"
    />
  </div>
</template>

<script>
import { timestampToUTCDate } from "@/utils/time";
import { joinUrlImg } from "@/utils/setUrlImg";
import filterPop from "@/views/nav/evaluate/evaluationRecord/components/filterPop.vue";
import recordOperation from "@/views/nav/evaluate/evaluationRecord/components/recordOperation.vue";
import searchInput from "@/components/searchInput/index.vue";
import {
  queryOnConRecordt,
  delOnConsultationRecord,
} from "@/api/consultation/record";
export default {
  data() {
    return {
      value: "",
      longPressTimer: null,
      isShowOperationPop: false,
      isShowFilter: false,
      pageSize: 8,
      page: 1,
      total: null,
      consultRecordList: [],
      filterInfo: {},
      currentInfo: {},
    };
  },
  components: {
    recordOperation,
    searchInput,
    filterPop,
  },
  created() {
    this.getOnConRecordt();
  },
  methods: {
    timestampToUTCDate,
    joinUrlImg,
    handleScroll(e) {
      const container = e.target;
      const containerHeight = container.clientHeight;
      const contentHeight = container.scrollHeight;
      const scrollTop = container.scrollTop;
      if (scrollTop + containerHeight >= contentHeight) {
        if (this.total > this.consultRecordList.length) {
          this.page += 1;
          this.getOnConRecordt();
        }
      }
    },
    checkDetails(item) {
      this.$router.push(
        `/consultationRecordDetail?id=${item.id}&consultId=${item.consult_id}&name=${item.consult_name}`
      );
    },
    async getOnConRecordt(shouldReset = false) {
      if (shouldReset) {
        this.consultRecordList = [];
        this.page = 1; // 重置页码
      }
      let par = {
        pageSize: this.pageSize,
        page: this.page,
        question_id: this.filterInfo.question_id,
        start_time: this.filterInfo.start_time,
        end_time: this.filterInfo.end_time,
        question: this.value,
        is_end: this.filterInfo.is_end,
      };
      let res = await queryOnConRecordt(par);
      if (res.code == 200) {
        this.consultRecordList.push(...res.data.data);
        this.total = res.data.total;
      }
    },

    closeFilterPop(val) {
      this.isShowFilter = false;
      this.filterInfo = val;
      this.getOnConRecordt(true);
    },
    buttonOperation(val) {
      this.isShowOperationPop = false;
      if (val == "del") {
        this.$popup({
          title: "提示",
          message: `<div>确定要删除本条记录吗？</div>`,
          confirmOperate: async () => {
            const params = {
              id: this.currentInfo.id,
            };
            let res = await delOnConsultationRecord(params);
            if (res.code == 200) {
              this.$toast.success("删除成功");
              this.getOnConRecordt(true);
            }
          },
        });
      } else if (val == "detail") {
        this.$router.push(`/consultantInfo?id=${this.currentInfo.consult_id}`);
      }
    },
    startLongPress(v) {
      this.currentInfo = v;
      this.longPressTimer = setTimeout(() => {
        this.isShowOperationPop = true;
      }, 500);
    },
    endLongPress() {
      clearTimeout(this.longPressTimer);
    },
    handleSearch(value) {
      this.value = value;
      if (this.searchTimer) {
        clearTimeout(this.searchTimer);
      }
      this.searchTimer = setTimeout(() => {
        this.page = 1;
        this.consultRecordList = [];
        this.getOnConRecordt();
      }, 500);
    },
    openPop() {
      this.$emit("openPop");
    },
  },
};
</script>

<style lang="less" scoped>
.dis_flex {
  display: flex;
  align-items: center;
}

.just_center {
  justify-content: center;
}

.just_between {
  justify-content: space-between;
}

.felx1 {
  flex: 1;
}

.consult_page {
}

.consult_cont {
  background: #ffffff;
}

.input_box {
  width: 100%;
  height: 50px;
  background: #ffffff;
  border-bottom: 1px solid #dddddd;
}

.search_input {
  width: 270px !important;
  height: 36.67px;
  margin: 0 10px 0 0;
  background: #f4f6fa;
}

.filtrate_txt {
  color: #333333;
  font-size: 14px;
  font-size: 14px;
}

.filtrate_img {
  width: 13.33px;
  height: 13.33px;
  margin-left: 5.67px;
}

.record_list {
  height: calc(100vh - 160px);
  overflow: hidden;
  overflow-y: scroll;
}

.record_list::-webkit-scrollbar {
  display: none;
}

.item_record {
  margin: 0 10px;
  padding: 13px 0;
  border-bottom: 1px solid #dddddd;

  &:last-child {
    border-bottom: 0;
  }
}

.img_box {
  position: relative;
}

.item_img {
  width: 53.33px;
  height: 53.33px;
  border-radius: 8.67px;
  margin-right: 10px;
}

.red_dot {
  position: absolute;
  left: 46.67px;
  top: -3px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #ff6161;
}

.item_name,
.item_age {
  color: #333333;
  font-size: 16px;
}

.item_age {
  margin-right: 9.33px;
}

.sex_img {
  width: 19.33px;
  height: 19.33px;
  margin: 0 4.33px 0 4.67px;
}

.item_state {
  width: 43.33px;
  height: 19.33px;
  line-height: 19.33px;
  text-align: center;
  border-radius: 3.33px;
  font-size: 11.33px;
  color: #ffffff;
}

.state_01 {
  background: linear-gradient(0deg, #ffffff -20%, rgba(255, 255, 255, 0) 20%),
    #00aeff;
}

.state_02 {
  background: #dddddd;
}

.item_time {
  font-size: 10.67px;
  color: #aaaaaa;
}

.item_info {
  font-size: 12.67px;
  color: #aaaaaa;
  margin-top: 13px;
}

.no_data_txt {
  font-size: 14px;
  margin: 32px auto;
  text-align: center;
}
</style>
